// MaxExporter.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "MaxExporter.h"

#include "Exporter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
//		any functions exported from this DLL which call into
//		MFC must have the AFX_MANAGE_STATE macro added at the
//		very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//


/*
 * Returns the description of the library (stored in the string table)
 */
__declspec( dllexport ) const TCHAR* LibDescription() 
{
	return _T("Visual Exporter");
}

/*
 * Returns the number of classes in the Library.
 */
__declspec( dllexport ) int LibNumberClasses() 
{
	return 1;
}

/*
* The class description class.
*/
class ExporterClassDesc:
	public ClassDesc 
{
public:
	virtual int				IsPublic() { return 1; }
	virtual void*			Create(BOOL loading = FALSE) { return new Exporter; } 
	virtual const TCHAR*	ClassName() { return _T("visual_exporter"); }
	virtual SClass_ID		SuperClassID() { return SCENE_EXPORT_CLASS_ID; } 
	virtual Class_ID		ClassID() { return EXP_CLASS_ID; }
	virtual const TCHAR*	Category() { return _T("Exporter"); }
};

/*
 * Returns the class description for the requested class.
 */
__declspec( dllexport ) ClassDesc* LibClassDesc(int i) 
{
	switch(i) 
	{
	case 0: 
		return new ExporterClassDesc();
	default: 
		return 0;
	}
}

/*
 * Returns the library version. (defined in mfxexp.hpp)
 */
__declspec( dllexport ) ULONG LibVersion() 
{
	return VERSION_3DSMAX;
}

// CMaxExporterApp

BEGIN_MESSAGE_MAP(CMaxExporterApp, CWinApp)
END_MESSAGE_MAP()


// CMaxExporterApp construction

CMaxExporterApp::CMaxExporterApp()
	:m_pD3D9(NULL),m_pDevice(NULL)
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CMaxExporterApp object

CMaxExporterApp theApp;


// CMaxExporterApp initialization

BOOL CMaxExporterApp::InitInstance()
{
	CWinApp::InitInstance();

	return TRUE;
}

LPDIRECT3DDEVICE9 CMaxExporterApp::GetDevice( void ) const
{
	return m_pDevice;
}

bool CMaxExporterApp::InitD3DDevice( void )
{
	if( m_pD3D9 != NULL )
		m_pD3D9->Release();

	m_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = 600;
	d3dpp.BackBufferHeight = 400;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

	if( m_pDevice != NULL )
		m_pDevice->Release();

	HRESULT hr = m_pD3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL , D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pDevice );

	return hr == D3D_OK;
}
